package com.tjjingpan.networktest

import android.util.Log
import org.xml.sax.Attributes
import org.xml.sax.helpers.DefaultHandler

class ContentHandler :DefaultHandler() {
    private var nodeMane = ""
    private lateinit var id :StringBuilder
    private lateinit var name:StringBuilder
    private lateinit var version:StringBuilder


    override fun startDocument() {
        id = java.lang.StringBuilder()
        name= java.lang.StringBuilder()
        version= java.lang.StringBuilder()
    }

    override fun startElement(uri: String?, localName: String, qName: String?, attributes: Attributes?
    ) {
        //记录当前节点名
        nodeMane = localName
        Log.d("ContentHandler","uri is $uri")
        Log.d("ContentHandler","localName is $localName")
        Log.d("ContentHandler","qName is $qName")
        Log.d("ContentHandler","attributes is $attributes")
    }

    override fun characters(ch: CharArray?, start: Int, length: Int) {
        //根据当前节点名判断将内容添加到哪一个StringBuilder对象中
        when(nodeMane){
            "id" -> id.append(ch,start,length)
            "name" -> name.append(ch,start,length)
            "version" -> version.append(ch,start,length)
        }
    }

    override fun endElement(uri: String?, localName: String?, qName: String?) {
        if("app" == localName){
            Log.d("ContentHandler","id is ${id.toString().trim()}")
            Log.d("ContentHandler","name is ${name.toString().trim()}")
            Log.d("ContentHandler","version is ${version.toString().trim()}")
            //最后要将StringBuilder清空
            id.setLength(0)
            name.setLength(0)
            version.setLength(0)
        }
    }

    override fun endDocument() {
        super.endDocument()
    }
}